package com.ali.common;

import java.util.*;

public class CollectionUtil {
//Collection

    /**
     * 获取集合的长度
     */
    public static int size(Collection<Object> collection) {
        return collection.size();
    }

    /**
     * 判断集合是否为空
     */
    public static boolean isEmpty(Collection<Object> collection) {
        return collection.isEmpty();
    }

    /**
     * 删除集合中的值
     */
    public static Collection<Object> remove(Collection<Object> collection, String value) {
        collection.remove(value);
        return collection;
    }

    /**
     * 删除集合中所有的值
     */
    public static Collection<Object> clear(Collection<Object> collection) {
        collection.clear();
        return collection;
    }

//List

    /**
     * 排序
     */
    public static void sort(List<String> list) {
        Collections.sort(list);
    }

    /**
     * 自定义排序 - 正序
     */
    public static void customizeSortAsc(List<Object> list) {
        Collections.sort(list, new Comparator<Object>() {
            @Override
            public int compare(Object o1, Object o2) {
                String str1 = (String) o1;
                String str2 = (String) o2;
                return str1.compareTo(str2);
            }
        });
    }

    /**
     * 自定义排序 - 倒序
     */
    public static void customizeSortDesc(List<Object> list) {
        Collections.sort(list, new Comparator<Object>() {
            @Override
            public int compare(Object o1, Object o2) {
                String str1 = (String) o1;
                String str2 = (String) o2;
                return str2.compareTo(str1);
            }
        });
    }

    /**
     * 使List变为不能修改
     */
    public static List<Object> unmodifiableList(List<Object> list) {
        return Collections.unmodifiableList(list);
    }

//Set

    /**
     * 使Set变为不能修改
     */
    public static Set<Object> unmodifiableSet(Set<Object> set) {
        return Collections.unmodifiableSet(set);
    }
}